package com.kzd.gtboon.main.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setTransitionStatusE
import com.kzd.gtboon.databinding.IncludeAppRefreshBinding
import com.kzd.gtboon.jetpack.vm.MineViewModel
import com.kzd.gtboon.main.home.adapter.CouponPriceAdapter
import com.kzd.gtboon.main.popup.CouponUsePopup
import com.kzd.gtboon.main.toGameDetailsActivity
import com.kzd.gtboon.main.toMainActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MineCouponFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>() {

    companion object {
        const val KEY_COLLECT_ACTION = "KEY_COLLECT_ACTION"
        fun newInstance(action: Int): MineCouponFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_ACTION, action)
            }
            return MineCouponFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mCouponUsePopup: CouponUsePopup by lazy {
        CouponUsePopup(requireContext())
    }

    private val mCouponPriceAdapter =
        CouponPriceAdapter(CouponPriceAdapter.Action.COUPON_PRICE_USE)

    private val mAction: Int by lazy {
        arguments?.getInt(KEY_COLLECT_ACTION) ?: 0
    }

    private val mViewModel:MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {

        mViewBinding.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mCouponPriceAdapter

        mCouponPriceAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mCouponPriceAdapter.getItem(position)
            if (view.id == R.id.tvCouponBottomUse) {
                mCouponUsePopup.fillData(itemData)
                    .showPopupWindow()
            } else if (view.id == R.id.tvCouponBottomDraw) {
                if (itemData.gid.toInt() != 0) {
                    toGameDetailsActivity(itemData.gid)
                } else {
                    toMainActivity(index = 1)
                }
            }
        }

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.mineCouponPrice(mAction)
        }
        mViewBinding.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.mineCouponPrice(mAction, isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.mineCouponPrice(mAction)
    }

    override fun initViewMode() {
        mViewModel.couponPriceLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response, mCouponPriceAdapter, mViewBinding.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mCouponPriceAdapter.addData(data)
            } else {
                mCouponPriceAdapter.setNewInstance(data)
            }
        })
    }

}